/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-08  18:15
 * @Description : 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 * @Version : 1.0
 */

public class Num021_exchange {
    public static int[] exchange(int[] nums) {
        int count = 1;
        int N = nums.length ;
        int m = 0;
        while (m < N) {
            if (nums[m] % 2 == 1) {
                swap(nums, 0, m);
                break;
            }
            m++;
        }
        for (int i = 1; i < N; i++) {
            if (nums[i] % 2 == 1) {
                swap(nums, i, count++);
            }
        }
        return nums;
    }

    private static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 6, 5, 4, 7, 8, 9};
        printArray(arr);
        int[] in = exchange(arr);
        printArray(in);
    }
}
